package com.lengyu.iottransfer.gateway.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lengyu.iottransfer.gateway.common.BasePageParamDto;
import com.lengyu.iottransfer.gateway.common.HttpResultDto;
import com.lengyu.iottransfer.gateway.convert.IUserConverter;
import com.lengyu.iottransfer.gateway.dto.UserDto;
import com.lengyu.iottransfer.gateway.entity.User;
import com.lengyu.iottransfer.gateway.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyu
 * @since 2023-01-18
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserConverter userConverter;

    @PreAuthorize("hasPermission('*', 'user_read')")
    @PostMapping(value="/add")
    public HttpResultDto<String> add(@RequestBody UserDto userDto){
        User user = userConverter.dtoToEntity(userDto);
        List<String> departList = userDto.getDepartList();
        List<String> roleList = userDto.getRoleList();

        if(StringUtils.isNotBlank(user.getId())){
            return HttpResultDto.failed("新建用户不应存在id");
        }
        userService.save(user);
        return HttpResultDto.success();
    }
    @PreAuthorize("hasPermission('*', 'user_edit')")
    @PostMapping(value="/update")
    public HttpResultDto<String> update(@RequestBody UserDto userDto){
        User user = userConverter.dtoToEntity(userDto);
        List<String> departList = userDto.getDepartList();
        List<String> roleList = userDto.getRoleList();

        if(StringUtils.isNotBlank(user.getId())){
            userService.updateById(user);
        }
        else{
            return HttpResultDto.failed("用户ID不存在无法更新");
        }

        return HttpResultDto.success();
    }
    @PreAuthorize("hasPermission('*', 'user_edit')")
    @PostMapping(value="/delete")
    public HttpResultDto<String> delete(String id){
        User user = userService.getById(id);
        if(user == null){
            return HttpResultDto.failed("用户ID不存在");
        }
        else{
            user.setDeleteFlag(true);
        }
        userService.updateById(user);
        return HttpResultDto.success();
    }

    //参数1(/user/page)指明了访问该接口需要的url，参数2®指明了访问该接口需要的权限。
    @PreAuthorize("hasPermission('*', 'user_read')")
    @PostMapping(value="/page")
    public HttpResultDto<Page<UserDto>> page(@RequestBody BasePageParamDto pageParamDto){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(pageParamDto.getParamMapObj() != null){
            for(String key:pageParamDto.getParamMapObj().keySet()){
                if(StringUtils.isNotBlank(String.valueOf(pageParamDto.getParamMapObj().get(key)))){
                    queryWrapper.like(key,pageParamDto.getParamMapObj().get(key));
                }
            }
        }
        Page<User> entityPage = userService.page(new Page<>(pageParamDto.getPageIndex(), pageParamDto.getPageSize()),queryWrapper);
        Page<UserDto> dtoPage = userConverter.pageEntityToDto(entityPage);
        return HttpResultDto.success(dtoPage);
    }

    @PreAuthorize("hasPermission('*', 'user_read')")
    @GetMapping(value="/detail")
    public HttpResultDto<UserDto> detail(String id){
        User user = userService.getById(id);
        UserDto userDto = userConverter.entityToDto(user);
        //TODO 获取各种权限信息
        return HttpResultDto.success(userDto);
    }

}
